#include "customnumedit.h"
#include "ui_customnumedit.h"
#include "customlineedit.h"
#include <QRegExp>
#include <QValidator>

CustomNumEdit::CustomNumEdit(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CustomNumEdit)
{
    ui->setupUi(this);
    this->initUi();
    this->init(INT_MIN, INT_MAX, 0);
}

CustomNumEdit::~CustomNumEdit()
{
    delete ui;
}

void CustomNumEdit::initUi()
{
    this->m_numEdit = new CustomLineEdit;
    this->ui->gridLayout->addWidget(this->m_numEdit);

    QRegExp regx("^[-+]?[0-9]*\\.?[0-9]+$");
    QValidator *validator = new QRegExpValidator(regx, this->m_numEdit);
    this->m_numEdit->setValidator(validator);

    connect(this->m_numEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged()));
    connect(this->m_numEdit, SIGNAL(focusIn()), this, SLOT(onFocusIn()));
    connect(this->m_numEdit, SIGNAL(focusOut()), this, SLOT(onFocusOut()));
}

void CustomNumEdit::init(double min, double max, int decimals, bool allowEmpty)
{
    this->m_min = qMin(min, max);
    this->m_max = qMax(min, max);
    this->m_decimals = qMax(decimals, 0);
    this->m_defaultValue = min;
    this->m_allowEmpty = allowEmpty;
    this->m_numEdit->setText(m_allowEmpty ? "" : QString::number(this->m_min, 'f', this->m_decimals));
}

void CustomNumEdit::init(double min, double max, double defaultValue, int decimals)
{
    this->m_min = qMin(min, max);
    this->m_max = qMax(min, max);
    this->m_decimals = qMax(decimals, 0);
    this->m_defaultValue = qMin(this->m_max, qMax(defaultValue, this->m_min));
    this->m_allowEmpty = false;
    setValue(defaultValue);
    setPlaceholderText(QString("%1-%2").arg(min).arg(max));
}

void CustomNumEdit::init(double min, double max, int defaultValue)
{
    init(min, max, (double)defaultValue, 0);
}

double CustomNumEdit::value() const
{
    return this->m_numEdit->text().toDouble();
}

double CustomNumEdit::setValue(double value)
{
    double tmp = qMin(this->m_max, qMax(value, this->m_min));
    this->m_numEdit->setText(QString::number(tmp, 'f', this->m_decimals));
    return this->value();
}

void CustomNumEdit::onTextChanged()
{
}

void CustomNumEdit::onFocusIn()
{
    this->m_value = (text().length() > 0) ? this->value() : this->m_defaultValue;
}

void CustomNumEdit::onFocusOut()
{
    setText(this->m_numEdit->text().trimmed());
    emit this->lostFocus();
}

QString CustomNumEdit::placeholderText() const
{
    return this->m_numEdit->placeholderText();
}

void CustomNumEdit::setPlaceholderText(const QString &text)
{
    this->m_numEdit->setPlaceholderText(text);
}

QString CustomNumEdit::text() const
{
    return this->m_numEdit->text();
}

bool CustomNumEdit::setProperty(const char *name, const QVariant &value)
{
    return this->m_numEdit->setProperty(name, value);
}

QVariant CustomNumEdit::property(const char *name) const
{
    return this->m_numEdit->property(name);
}

void CustomNumEdit::setText(const QString &text)
{
    if (text.isEmpty())
    {
        this->m_numEdit->setText(m_allowEmpty ? text : QString::number(this->m_value, 'f', this->m_decimals));
    }
    else
    {
        setValue(text.toDouble());
    }
}

void CustomNumEdit::clear()
{
    this->m_numEdit->setText(m_allowEmpty ? "" : QString::number(this->m_value, 'f', this->m_decimals));
}

void CustomNumEdit::setFocus()
{
    this->m_numEdit->setFocus();
}

void CustomNumEdit::selectAll()
{
    this->m_numEdit->selectAll();
    this->m_numEdit->setFocus();
}
